package richbar.com.richbar.fragment.collect;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.common.base.BaseActivity;
import com.common.base.BaseFragment;
import com.common.help.RecycleViewHelp;
import com.common.util.LogUtil;
import com.common.util.StringUtil;
import com.okhttp.GlideUtil;
import com.okhttp.network.HttpUtils;
import com.okhttp.network.MyGson;

import java.util.ArrayList;
import java.util.List;

import richbar.com.richbar.R;
import richbar.com.richbar.activity.product.ShopAc;
import richbar.com.richbar.base.BaseApplication;
import richbar.com.richbar.bean.GoodsCollectResult;
import richbar.com.richbar.bean.RateResult;
import richbar.com.richbar.util.NetWorkUtil;
import richbar.com.richbar.util.Util;

/**
 * Created by QiYang on 2018/11/30.
 */

public class ShopFragmet extends BaseFragment {
    private SwipeRefreshLayout swipe;
    private RecyclerView recyclerView;
    private MyAdapter adapter;
    private boolean isCanEditor = false;
    private List<String> selects = new ArrayList<>();
    @Override
    protected View onCreate(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods,container,false);
        swipe = view.findViewById(R.id.swipe);
        recyclerView = view.findViewById(R.id.recycleView);
        swipe.setRefreshing(true);
        swipe.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(false);
            }
        });
        return view;
    }

    @Override
    protected void initData() {
        adapter = new MyAdapter(R.layout.item_shop_collect,new ArrayList<GoodsCollectResult.DataEntity.RowsEntity>());
        RecycleViewHelp.setManager(recyclerView,getActivity());
        recyclerView.setAdapter(adapter);
        adapter.openLoadMore(1,true);
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                getData(true);
            }
        });
        getData(false);
    }

    private void getData(final boolean loadMore) {
        if (loadMore){
            pageAdd();
        }else {
            pageReset();
        }
        NetWorkUtil.sellerList(currentPage(), "10", BaseApplication.uid, Util.md5(), new HttpUtils.HttpCallback() {
            @Override
            public void onSuccess(String data) {
                final GoodsCollectResult result = MyGson.getInstance().fromJson(data, GoodsCollectResult.class);
                refreshDismiss(swipe);
                if (checkIsOk(result)){
                    setData(result,loadMore);
                }
            }
        });
    }

    private void setData(GoodsCollectResult result, boolean loadMore) {
        /*List<GoodsCollectResult.DataEntity.RowsEntity> data = result.getData().getRows();
        adapter.setNewData(data);*/

        if (page > result.getData().getTotalpage()){
            adapter.isNextLoad(false);
            LogUtil.e("DDDDDDDDDDD",1);
            return;
        }
        List<GoodsCollectResult.DataEntity.RowsEntity> allData = adapter.getData();
        if (loadMore) {
            LogUtil.e("DDDDDDDDDDD",4);
            allData.addAll(result.getData().getRows());
            adapter.isNextLoad(true);
        } else {
            LogUtil.e("DDDDDDDDDDD",5);
            allData = result.getData().getRows();
        }
        adapter.setNewData(allData);
    }

    public void editor(){
        isCanEditor = true;
        selects = new ArrayList<>();
        adapter.notifyDataSetChanged();
    }

    public void noEditor(){
        isCanEditor = false;
        if (adapter != null) {
            adapter.notifyDataSetChanged();
        }
    }

    public void refresh(){
        if (null != adapter) {
            selects = new ArrayList<>();
            getData(false);
        }
    }

    public String getSelects(){
        if (selects != null && selects.size() > 0){
            return StringUtil.listToStr(selects);
        }else {
            return "";
        }
    }

    class MyAdapter extends BaseQuickAdapter<GoodsCollectResult.DataEntity.RowsEntity> {
        public MyAdapter(int layoutResId, List<GoodsCollectResult.DataEntity.RowsEntity> data) {
            super(layoutResId, data);
            LogUtil.e("KKKKKKKKKK",selects);
        }

        @Override
        protected void convert(final BaseViewHolder holder, final GoodsCollectResult.DataEntity.RowsEntity bean) {
            final ImageView select = holder.getView(R.id.select);
            if (isCanEditor){
                select.setVisibility(View.VISIBLE);
            }else {
                select.setVisibility(View.GONE);
            }
            holder.setText(R.id.name,bean.getSeller_name());
            holder.setText(R.id.num,bean.getSeller_id() + "人收藏");
            ImageView imageView = holder.getView(R.id.iv);
            GlideUtil.loadImage(getActivity(), NetWorkUtil.URL + bean.getSeller_logo(),imageView);
            holder.setOnClickListener(R.id.select, new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (selects.contains(bean.getColl_id())){
                        select.setImageResource(R.mipmap.unselect1);
                        selects.remove(bean.getColl_id());
                    }else {
                        select.setImageResource(R.mipmap.select);
                        selects.add(bean.getColl_id());
                    }
                    notifyDataSetChanged();
                }
            });
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    start(ShopAc.class, new BaseActivity.OnBundleListener() {
                        @Override
                        public Bundle onSetBundle(Bundle bundle) {
                            bundle.putString("id",bean.getSeller_id());
                            return bundle;
                        }
                    });
                }
            });
        }
    }
}
